package uloha1;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Riesenie {

    List<Batoh> zoznamInstancii;

    public Riesenie(String menoSuboru) {
        zoznamInstancii = new ArrayList<Batoh>();
        nacitajVstupnySuborSInstanciou(menoSuboru);
    }

    public void nacitajVstupnySuborSInstanciou(String menoSuboru) {
        File nacitavanySubor;
        FileInputStream fstream = null;
        DataInputStream in = null;
        BufferedReader br = null;

        String nacitaneData;
        String kopiaDat;
        
        int cenaVeci = 0;
        int vahaVeci = 0;
        int cisloInstance = 0;
        int pocetVeciVBatohu = 0;
        int maxNosnostBatohu = 0;

        try {
            fstream = new FileInputStream(menoSuboru);
            in = new DataInputStream(fstream);
            br = new BufferedReader(new InputStreamReader(in));
            while ((nacitaneData = br.readLine()) != (null) ) {
                Batoh aktualnaInstancia = null;
                Scanner sc = new Scanner(nacitaneData).useDelimiter(" ");
                cisloInstance = sc.nextInt();
                pocetVeciVBatohu = sc.nextInt();
                //System.out.println("Veci je " +pocetVeciVBatohu);
                maxNosnostBatohu = sc.nextInt();
                aktualnaInstancia = new Batoh(maxNosnostBatohu, pocetVeciVBatohu,cisloInstance);

                for (int i = 0; i < pocetVeciVBatohu; i++) {
                    vahaVeci = sc.nextInt();
                    cenaVeci = sc.nextInt();
                    //System.out.println("Vytvorim objekt, cena je" + cenaVeci + " a vaha veci je " + vahaVeci);
                    VecDoBatohu vkladanaVec = new VecDoBatohu(cenaVeci, vahaVeci);
                    aktualnaInstancia.pridajVecDoBatohu(i + 1, vkladanaVec);
                } //vsetky veci su uz priradene do batohu, instancia je nacitana
                zoznamInstancii.add(aktualnaInstancia);
                //System.out.println("Batoh " + aktualnaInstancia.maxHmotnost + " " + aktualnaInstancia.pocetVeciVbatohu + "  bol prave vytvoreny");
            }

        } catch (FileNotFoundException ex) {
            System.out.println("Subor zadany ako argument nebol najdeny");
            Logger.getLogger(Riesenie.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Riesenie.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (Exception ex) {
            System.out.println("Some exception");
        }
        finally {
            try {
                fstream.close();
                in.close();
                br.close();
                
            } catch (IOException ex) {
                Logger.getLogger(Riesenie.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

//    public static void main(String[] args) {
//    long start = System.currentTimeMillis();
//    File file = new File("sol");
//    if (!file.exists())
//    {
//        file.mkdir();
//    }
//        if (args.length != 1) {
//            System.out.println(args.length);
//            System.out.println("Chybny vstup - nebol zadany vstupny parameter");
//            return;
//        }
//        System.out.println("Hruba sila");
//        Riesenie riesenieInstancii = new Riesenie(args[0]);
//        Iterator iteruj = riesenieInstancii.zoznamInstancii.iterator();
//        while (iteruj.hasNext()) {
//            Batoh ruksak = (Batoh) iteruj.next();
//            ruksak.vyriesProblemAvypisRiesenie();
//        }
//        long stop = System.currentTimeMillis();
//        
//        System.out.println( "Cas je "+((double)(stop-start)/(double)1000)+ " sekund" );
//    } // nacitanie vsetkych instancii a ich postupne vyriesenie
}
